package com.talon.exo_player;

import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ui.StyledPlayerView;
import com.talon.lib_base.ConfigApi;

public class ExoPlayerActivity extends AppCompatActivity {
    StyledPlayerView mPlayerView;
    ExoPlayer mSimpleExoPlayer;
    public static String videoUrl = ConfigApi.videoUrl;
    SurfaceView surfaceView;
    private long time = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.talon.exo_player.R.layout.activity_exo_player);
        mPlayerView = findViewById(com.talon.exo_player.R.id.player_view);
        surfaceView = findViewById(com.talon.exo_player.R.id.surface_view);
        mSimpleExoPlayer = new ExoPlayer.Builder(/* context= */ this)
                .build();


        mPlayerView.setPlayer(mSimpleExoPlayer);
        mPlayerView.setKeepScreenOn(true);
        // Build the media item.
        MediaItem mediaItem = MediaItem.fromUri(videoUrl);
        // Set the media item to be played.
        mSimpleExoPlayer.setMediaItem(mediaItem);
        //设置播放器是否当装备好就播放， 如果看源码可以看出，ExoPlayer的play()方法也是调用的这个方法
        mSimpleExoPlayer.setPlayWhenReady(true);
        //资源准备，如果设置 setPlayWhenReady(true) 则资源准备好就立马播放。

        mSimpleExoPlayer.addListener(new Player.Listener() {
            @Override
            public void onPlaybackStateChanged(int playbackState) {
                // switch (playbackState){
                if (playbackState == 3) {
                    float result = System.currentTimeMillis() - time;
                    Log.i("xiaote", "exo player~~~~~playbackState :time  ::" + result);
                }
                //}
            }

        });
        time = System.currentTimeMillis();
        mSimpleExoPlayer.prepare();
        mSimpleExoPlayer.play();


    }


    @Override
    protected void onPause() {
        super.onPause();
        mSimpleExoPlayer.setPlayWhenReady(false);
        mSimpleExoPlayer.getPlaybackState();
    }


}